package com.cold.demo.template.pojo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@ApiModel("00、通用接口返回模型")
public class Res<T> {
    @ApiModelProperty("数据信息")
    private T data;

    @ApiModelProperty("状态码")
    private int code;

    @ApiModelProperty("提示信息")
    private String msg;


    private static <T> Res<T> create(T data, int code, String msg) {
        Res<T> res = new Res<>();
        res.setData(data);
        res.setCode(code);
        res.setMsg(msg);
        return res;
    }

    public static <T> Res<T> success(T data) {
        StatusCode success = StatusCode.SUCCESS;
        return create(data, success.getCode(), success.getMsg());
    }

    public static <T> Res<T> success() {
        return success(null);
    }

    // ================================================================================================================
    public static <T> Res<T> error(String msg) {
        StatusCode error = StatusCode.ERROR;
        return error(error.getCode(), msg);
    }

    public static <T> Res<T> error(int code, String msg) {
        return create(null, code, msg);
    }
    // ================================================================================================================


}
